package com.fm.service.impl;

import com.fm.common.ResultResponse;
import com.fm.dao.MessageMapper;
import com.fm.pojo.Message;
import com.fm.service.IMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("messageService")
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private MessageMapper messageMapper;

    public ResultResponse getMessage(String messageId){
        Message message = messageMapper.selectByPrimaryKey(messageId);
        if(message!=null){
            //改变信息已读状态
            int resultCount = messageMapper.updateStatus(messageId);
            if(resultCount!=0){
                return ResultResponse.isSuccess(message);
            }
        }
        return ResultResponse.isErrorMessage("该消息不存在");
    }

    public ResultResponse<String> delMessage(String messageId){
        int resultCount = messageMapper.deleteByPrimaryKey(messageId);
        if(resultCount == 0){
            return ResultResponse.isErrorMessage("删除失败");
        }
        return ResultResponse.isSuccessMessage("删除成功");
    }

    public ResultResponse<PageInfo> messageList(String userId,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Message> list = messageMapper.selectToUser(userId);
        PageInfo pageInfo = new PageInfo(list);
        return ResultResponse.isSuccess(pageInfo);
    }

    public ResultResponse<String> addMessage(Message message){
        int resultCount = messageMapper.insert(message);
        if(resultCount == 0){
            return ResultResponse.isErrorMessage("留言失败");
        }
        return ResultResponse.isSuccessMessage("留言成功");
    }
}
